package com.aynu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.aynu.gulimall.product.entity.AttrEntity;
import com.aynu.gulimall.product.service.AttrAttrgroupRelationService;
import com.aynu.gulimall.product.service.AttrService;
import com.aynu.gulimall.product.service.CategoryService;
import com.aynu.gulimall.product.vo.AttrGroupRelationVo;
import com.aynu.gulimall.product.vo.AttrGroupwithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.aynu.gulimall.product.entity.AttrGroupEntity;
import com.aynu.gulimall.product.service.AttrGroupService;
import com.aynu.common.utils.PageUtils;
import com.aynu.common.utils.R;



/**
 * 属性分组
 *
 * @author majun
 * @email mjun1833@foxmail.com
 * @date 2020-06-06 21:39:56
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     * \@PathVariable路径变量，需要加
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }

    /**
     * /product/attrgroup/{catelogId}/withattr
     * @return
     * 获取分类下的属性分组的全部的规格参数（平台属性-商品维护-发布商品-2.规格参数）
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
        //1、查出当前分类下的属性分组
        //2、查出分组下的规格参数
        List<AttrGroupwithAttrVo> vos = attrGroupService.getAttrGroupWithAttr(catelogId);

        return R.ok().put("data",vos);
    }
    /*
     * /product/attrgroup/attr/relation/delete
     * 属性分组-关联-移出
     * */

    @PostMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * /product/attrgroup/attr/relation
     *属性分组-关联-新建关联-确认新增
     * @return
     */
    @PostMapping("/attr/relation")
    public R newRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    /*
    /product/attrgroup/{attrgroupId}/noattr/relation
    * 获取属性分组没有关联的列表(属性分组-关联-新建关联)
    * */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R noAttrRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    /*
     * 获取属性分组关联的列表
     * */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId")Long attrgroupId){
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		//按分类id查询改分组的全部路径
		Long[] path = categoryService.findCatelogPath(attrGroup.getCatelogId());
		attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
